package com.xique.park.bean.transform.web.garageLaneDev;

import com.xique.common.core.utils.StringUtils;
import com.xique.park.bean.vo.web.garageLaneDeviceTree.TreeCommVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class ParkGarageLaneDeviceTransform {

    /**
     * 数据库查出实体转化为车库实体
     * @param list
     * @return
     */
    public static List<TreeCommVo> transformTreeGarageVo(List<Map<String, Object>> list){
        List<TreeCommVo> garageVoList = new ArrayList<>();
        List<Long> garageIds = new ArrayList<>();
        for(Map<String,Object> item : list){
            Integer garageId = (Integer) item.get("garageId");
            if(null != garageId){
                //车库
                if(!garageIds.contains(garageId.longValue())){
                    TreeCommVo garageVo = new TreeCommVo();
                    garageVo.setId(garageId.longValue());
                    garageVo.setLabel((String)item.get("garageName"));
                    garageVo.setType("garage");
                    garageVo.setIcon("el-icon-high-park");
                    garageVo.setPid(null != item.get("garagePid") ? ((Integer) item.get("garagePid")).longValue() : 0L);
                    garageVoList.add(garageVo);
                    garageIds.add(garageId.longValue());
                }
            }
        }
        return garageVoList;
    }

    /**
     * 车库id-通道
     * @param list
     * @return
     */
    public static Map<Long,List<TreeCommVo>> transformTreeLaneVo(List<Map<String, Object>> list){
        Map<Long,List<TreeCommVo>> laneListMap = new HashMap<>();
        Map<Long,List<TreeCommVo>> deviceListMap = transformTreeDeviceVo(list);
        List<Integer> laneIdList = new ArrayList<>();
        for(Map<String,Object> item : list){
            Integer garageId = (Integer) item.get("garageId");
            Integer laneId = (Integer)item.get("laneId");
            if(null != laneId){
                if(!laneIdList.contains(laneId)){
                    //通道
                    List<TreeCommVo> laneVoList = laneListMap.get(garageId.longValue());
                    if(CollectionUtils.isEmpty(laneVoList)){
                        laneVoList = new ArrayList<>();
                    }
                    TreeCommVo laneVo = new TreeCommVo();
                    laneVo.setId(laneId.longValue());
                    laneVo.setLabel((String)item.get("laneName"));
                    laneVo.setType("lane");
                    laneVo.setIcon("el-icon-high-door");
                    laneVo.setPid(garageId.longValue());
                    if(CollectionUtils.isNotEmpty(deviceListMap.get(laneId.longValue()))){
                        laneVo.setChildren(deviceListMap.get(laneId.longValue()));
                    }
                    laneVoList.add(laneVo);
                    laneListMap.put(garageId.longValue(),laneVoList);
                    laneIdList.add(laneId);
                }
            }
        }
        return laneListMap;
    }

    /**
     * 通道id-（相机以及其他附加设备）
     * @param list
     * @return
     */
    public static Map<Long,List<TreeCommVo>> transformTreeDeviceVo(List<Map<String, Object>> list){
        Map<Long,List<TreeCommVo>> deviceListMap = new HashMap<>();
        List<Integer> subIds = new ArrayList<>();
        List<Integer> devIds = new ArrayList<>();
        for(Map<String,Object> item : list){
            Integer laneId = (Integer)item.get("laneId");
            if(null != laneId){
                //相机
                Integer devId = (Integer)item.get("devId");
                if(null != devId && !devIds.contains(devId)){
                    List<TreeCommVo> ipcVoList = deviceListMap.get(laneId.longValue());
                    if(CollectionUtils.isEmpty(ipcVoList)){
                        ipcVoList = new ArrayList<>();
                    }
                    TreeCommVo ipcVo = new TreeCommVo();
                    ipcVo.setId(devId.longValue());
                    //区分主相机，副相机
                    if(StringUtils.isNotEmpty((String)item.get("ipcIsMain")) && ((String)item.get("ipcIsMain")).equals("Y")){
                        ipcVo.setType("ipc");
                        ipcVo.setIcon("el-icon-high-camera");
                    }else{
                        ipcVo.setType("ipc-sub");
                        ipcVo.setIcon("el-icon-high-equipt");
                    }
                    ipcVo.setPid(laneId.longValue());
                    ipcVo.setLabel((String)item.get("devName"));

                    ipcVoList.add(ipcVo);
                    deviceListMap.put(laneId.longValue(),ipcVoList);
                    devIds.add(devId);
                }
                //其他附加设备
                Integer subId = (Integer)item.get("subId");
                if(null != subId && !subIds.contains(subId)){
                    List<TreeCommVo> subDevVoList = deviceListMap.get(laneId.longValue());
                    if(CollectionUtils.isEmpty(subDevVoList)){
                        subDevVoList = new ArrayList<>();
                    }
                    TreeCommVo subDevVo = new TreeCommVo();
                    subDevVo.setId(subId.longValue());
                    subDevVo.setType((String) item.get("subDevType"));
                    subDevVo.setPid(laneId.longValue());
                    subDevVo.setLabel((String)item.get("subDevName"));
                    subDevVo.setIcon("el-icon-high-equipt");
                    subDevVoList.add(subDevVo);
                    deviceListMap.put(laneId.longValue(),subDevVoList);
                    subIds.add(subId);
                }
            }
        }
        return deviceListMap;
    }

}
